using UnityEngine;
using System.Collections;


public delegate void OnMouseEvent(object obj);

public class HUDButton : Element {

    public event OnMouseEvent MouseClick;
    public event OnMouseEvent MouseOverEvent;
    public event OnMouseEvent MouseRelease;

    private string _name;

    public string Name
    {
        get { return _name; }
    }

    private ButtonTexture buttonTexture;
    public ButtonTexture.StateButton _state;
    public ButtonTexture.StateButton _nextState;
    private ButtonInfo currentButton;
    private bool animPlay;
    private string baseButtonName = "";


    public void SetName(string name, ButtonTexture.StateButton state = ButtonTexture.StateButton.NORMAL, string baseButton = "")
    {
        _name = name;
        baseButtonName = baseButton;

        if (baseButton != "")
        {
            buttonTexture = TextureManager.Instance().GetButtonTexture(baseButton);
        }
        else
            buttonTexture = TextureManager.Instance().GetButtonTexture(_name);

        currentButton = buttonTexture.GetButtonInfo(state);
        SetState(state, true);
    }

    //Set State Button 
    public bool SetState(ButtonTexture.StateButton state, bool forceState)
    {
        if (state != ButtonTexture.StateButton.LOCK)
        {
            if (_state == ButtonTexture.StateButton.LOCK && state != ButtonTexture.StateButton.UNLOCK)
                return false;

            if (_nextState == ButtonTexture.StateButton.LOCK && state != ButtonTexture.StateButton.UNLOCK)
                return false;
        }

        //Allreeady unlock
        if (state == ButtonTexture.StateButton.UNLOCK && _state != ButtonTexture.StateButton.LOCK)
        {
            state = ButtonTexture.StateButton.NORMAL;
        }
        

        if (currentButton == null)
            return false;

        //Must wait to change state
        if (!forceState && currentButton._nextWait)
        {
            _nextState = state;
            return false;
        }

        _state = state;

        //Launch Animation & set texture
        PlayAnim(_state, buttonTexture.GetButtonInfo(_state));
        return true;
    }

    public void SetNextState(ButtonTexture.StateButton nextState)
    {
        _nextState = nextState;
    }

    //ClickOn
    public override void ClickOn()
    {
        if(MouseClick != null)
            MouseClick(this);
    }

    //Drag On
    public override void DragOn()
    {

    }

    //ClickRelease
    public override void ClickRelease()
    {        

        if (SetState(ButtonTexture.StateButton.RELEASE, false))
        {
            if (MouseRelease != null)
                MouseRelease(this);
        }
    }

    //Mouse Over
    public override void MouseOver()
    {
        

        if (_state != ButtonTexture.StateButton.OVER)
        {
            if(SetState(ButtonTexture.StateButton.OVER, false))
            {
                if(MouseOverEvent != null)
                    MouseOverEvent(this);
            }
        }
    }

    //Mouse QUit
    public override void MouseQuit()
    {        
        SetState(ButtonTexture.StateButton.NORMAL, false);
    }




    //Launch Anim correspond at state
    public void PlayAnim(ButtonTexture.StateButton state, ButtonInfo infos)
    {
        currentButton = infos;

        if (buttonTexture == null || infos == null)
            return;

        
        Texture textureButton = infos._texture;//buttonTexture.GetTexture(state);

        if (textureButton == null)
        {
            return;
        }

        this.renderer.material.SetTexture("_MainTex", textureButton);
        animPlay = true;

        if (!this.GetComponent<AnimatedButton>())
            this.gameObject.AddComponent<AnimatedButton>();

        this.GetComponent<AnimatedButton>().Play(infos._nbAnim, infos._loop);
    }


    //Call when anim is done
    public void AnimEnd()
    {

        SetState(_nextState, true);
        
    }

}
